home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Trading on the Edge
/
Trading On The Edge - CD-ROM Toolkit (Wayzata Technology)(2031)(1994).bin
/
mac
/
Shared
/
AZTE programs
/
inet.c
< prev
next >
Wrap
Text File
|
1993-06-11
|
4KB
|
163 lines
/* 7/17/89.
* Routine to set up a socket and connect to the monitor.
* Included into dani.c, inquire.c, and into control.c if INETBASED.
* Returns 0 if OK, 1 if no game is being played. Calls error() if error.
*
* You might possibly have to include a special library at link time to
* get this to work. E.g. on a Cray you need -lnet. Ask a local expert
* what library you need for 'socket' and 'connect' if you have a
* problem.
*/
extern void getpassword();
#ifdef VMS
#define ERRNO uerrno
#else
#define ERRNO errno
#endif
#define MAXNAME 32 /* must be < BUFSIZE in monitor's network.c */
/*#define PORT 12345*/ /* port number for monitor */
/* defined in define.h now */
int
inetsetup(hostnam,portnum,dsply)
char *hostnam;
int portnum;
int dsply;
{
int sock;
struct sockaddr_in server;
static char name[MAXNAME];
static char password[MAXNAME];
char msg[100];
char *mptr, *p;
long int hostaddr; /* must be 32 bits */
extern int ERRNO;
struct hostent *blob;
struct in_addr *host_address;
/* Zero the whole struct sockaddr_in server.
* If you don't have bzero() and bcopy(), look for memset() and memcpy()
* respectively, but note that source and destination are reversed in
* memcpy(), and that memset() has an extra argument (set it to 0)
*/
bzero((char*)&server, sizeof(server));
/* Put the network address of the DA monitor into the server structure.
* This uses the sfi.santafe.edu address unless you have the full
* networking software (file anyhost.c) and define ANYHOST
*/
#ifdef ANYHOST
anyhost(&server,&hostnam);
#else
/*
if (hostnam != NULL) {
sprintf(msg,"can only connect to %s",MONITORHOST);
error(msg);
}
*/
if (hostnam == NULL)
hostnam = MONITORHOST;
if ((blob = gethostbyname(hostnam)) == NULL) {
sprintf(msg,"Can not resolve %s", hostnam);
error(msg);
}
#ifdef CRAY
/* server.sin_addr.s_addr = hostaddr; */
bcopy(blob->h_addr,(char *)&server.sin_addr,4);
#else
/* bcopy(&hostaddr,(char *)&server.sin_addr,4); */
bcopy(blob->h_addr,(char *)&server.sin_addr,4);
#endif
#endif
/* fill in the address family and port number */
server.sin_family = AF_INET;
server.sin_port = portnum ? htons(portnum) : htons(PORT);
/* create socket */
sock = socket(AF_INET,SOCK_STREAM,0);
if (sock < 0)
error("couldn't create socket");
/* get name and role */
getpname(name,MAXNAME);
role = getrole();
/* tell user we're trying */
fprintf(stderr,"Trying %s ...\n",inet_ntoa(server.sin_addr));
/* connect to monitor.
* If this fails, first check that you're using the right library -- ASK
* AN EXPERT FOR YOUR SYSTEM. Note that getting "Connection refused"
* is normal if the monitor isn't runing -- it's no action or a timeout
* that signifies a real problem.
*/
ERRNO = 0;
if (connect(sock, (struct sockaddr *)&server, sizeof(server)) < 0) {
if (ERRNO > 0) perror("** failed to connect");
if (ERRNO == ECONNREFUSED)
sprintf(msg,"the monitor isn't running at %s",hostnam);
else
sprintf(msg,"couldn't connect to %s",hostnam);
error(msg);
}
fprintf(stderr,"-- Connected to %s --\n",hostnam);
/* assign input and output file descriptors */
outfd = sock;
infd = sock;
p = name;
while(*p && *p != '\n') p++;
*p = '\0';
/* send header with buyer/seller/either role, human/machine, and name */
sprintf(msg,"DA %d %d %8s\n",role,hmtype,name);
WRITE(outfd,msg,strlen(msg));
/* WRITE(outfd,name,strlen(name)); */
getpassword(password,MAXNAME);
WRITE(outfd,password,strlen(password));
/* echo messages from the monitor until 'start' or 'abort' or 'nogame' */
#ifdef VMS
if (hmtype==1) putchar('\n');
#endif
while (1) {
mptr = msg;
while (READ(infd,mptr,1) == 1 && *mptr != '\n') mptr++;
if (*mptr != '\n')
error("connection broken");
*++mptr = EOS;
if (strcmp(msg,"gamedata\n") == 0) {
get_gamedata(infd);
continue;
}
if (strcmp(msg,"players\n") == 0) {
get_players(infd);
continue;
}
if (strcmp(msg,"gamehist\n") == 0) {
get_gamehist(infd);
continue;
}
if (strcmp(msg,"start\n") == 0) break;
if (strcmp(msg,"abort\n") == 0 || strcmp(msg,"nogame\n") == 0) {
if (!dsply) {
if (*msg == 'a')
error("game aborted by monitor");
else
error("no game being played");
}
return 1;
}
if (dsply)
fputs(msg,stderr);
}
return 0;
}